import { h, ref } from 'vue';
import { cloneDeep } from 'lodash-es';
import { FormSchema } from '@/components/Form';

export class State {
  public id = 0; // id
  public name = ''; // 店铺名称
  public logo = ''; // LOGO
  public rating = 0; // 评分
  public reviews = 0; // 评论数
  public category = ''; // 行业分类
  public tags = null; // 标签
  public isCollected = 0; // 是否推荐
  public latitude = ''; // 经度
  public longitude = ''; // 纬度
  public address = ''; // 地址
  public contactPhone = ''; // 联系电话
  public intro = ''; // 简介
  public createdAt = ''; // 创建时间
  public updatedAt = ''; // 更新时间

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  name: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入店铺名称',
  },
  latitude: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入经度',
  },
  longitude: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入纬度',
  },
  address: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入地址',
  },
};

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  {
    field: 'name',
    component: 'NInput',
    label: '店铺名称',
    componentProps: {
      placeholder: '请输入店铺名称',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: 'id',
    key: 'id',
    align: 'left',
    width: -1,
  },
  {
    title: '店铺名称',
    key: 'name',
    align: 'left',
    width: -1,
  },
  {
    title: '行业分类',
    key: 'category',
    align: 'left',
    width: -1,
  },
  {
    title: '地址',
    key: 'address',
    align: 'left',
    width: -1,
  },
  {
    title: '联系电话',
    key: 'contactPhone',
    align: 'left',
    width: -1,
  },
  {
    title: '创建时间',
    key: 'createdAt',
    align: 'left',
    width: -1,
  },
  {
    title: '更新时间',
    key: 'updatedAt',
    align: 'left',
    width: -1,
  },
];